package com.wonyesoft.wuliu.acticity;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.utils.StringUtil;
import com.wonyesoft.wuliu.acticity.utils.StringUtils;
import com.wonyesoft.wuliu.acticity.utils.ToastUtils;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.State;

public class UserChangePwdActivity extends BaseActivity implements OnClickListener {

	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right;
	private ImageView head_left;
	private EditText password_yuan,password_new,password_zaici;
	private Button password_queren;
	private String passwordString,rePwdString,repwd2String;

	private RequestParams params;

	public static Gson gson = new Gson();

	private HttpUtils http;

	private AppContent app;
	
	
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_newpassword);
		password_zaici = (EditText) findViewById(R.id.password_zaici);
		password_new = (EditText) findViewById(R.id.password_zaici);
		password_yuan = (EditText) findViewById(R.id.password_yuan);
		password_queren = (Button) findViewById(R.id.password_queren);
		title = (TextView) findViewById(R.id.head_title);
		head_left = (ImageView) findViewById(R.id.head_left);
		head_left.setVisibility(0);
		title.setText("密码管理");
		http = new HttpUtils();
		head_left.setOnClickListener(this);
		password_queren.setOnClickListener(this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.password_queren:
			changePw();
		default:
			break;
		}
	}

	private void changePw() {
		passwordString = password_yuan.getText().toString();
		rePwdString = password_new.getText().toString();
		repwd2String = password_zaici.getText().toString();
		if(validate()) {
                   upUserInfo(passwordString, rePwdString);
		}
		
	}

	public boolean validate() {
		String pwd = null;
		String repwd = null;
		if (StringUtil.isEmpty(passwordString)) {
			ToastUtils.showLong("请输入原密码");
			password_new.requestFocus();
			return false;
		} 
		if (StringUtils.isEmpty(rePwdString)) {
			ToastUtils.showLong("请输入新密码");
			password_new.requestFocus();
			return false;
		} else {
			pwd = password_new.getText().toString().trim();
			if (pwd.length() < 6) {
				ToastUtils.showLong("新密码不能小于6位");
				password_new.requestFocus();
				return false;
			}
		}
		if (StringUtils.isEmpty(rePwdString)) {
			ToastUtils.showLong("请重复输入一次密码");
			password_new.requestFocus();
			return false;
		} else {
			repwd = password_zaici.getText().toString().trim();
			if (!pwd.equals(repwd)) {
				ToastUtils.showLong("两次输入的密码不一致");
				password_zaici.requestFocus();
				return false;
			}
		}
		return true;
	}
	// 更新用户密码
			private void upUserInfo(String infoName, String value
					) {

				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put("oldPassword", infoName);
				dataMap.put("newPassword", value);
				JsonRequest jr = JsonRequest.getInit();
				jr.setData(dataMap);
				String json = gson.toJson(jr);
				params = new RequestParams();
				params.addBodyParameter("jsonParam", json);
				// http.configCookieStore(app.getCookie());
				//System.out.println("changepwd:"+ AppContent.getPreferencesCookieStore().getCookie("JSESSIONID"));
				http.configCookieStore(AppContent.getPreferencesCookieStore());
				http.send(HttpRequest.HttpMethod.POST, GagApi.PWDCHANGE, params,
						new RequestCallBack<String>() {

							@Override
							public void onStart() {
								// testTextView.setText("conn...");
							}

							@Override
							public void onLoading(long total, long current,
									boolean isUploading) {
								if (isUploading) {
									// testTextView.setText("upload: " + current + "/" +
									// total);
								} else {
									// testTextView.setText("reply: " + current + "/" +
									// total);
								}
							}

							@Override
							public void onSuccess(ResponseInfo<String> responseInfo) {

								System.out.println("进入更新过程。。。。");
								Gson gson2 = new Gson();
								com.wonyesoft.wuliu.vo.JsonResponse jrs = gson2.fromJson(responseInfo.result,
										com.wonyesoft.wuliu.vo.JsonResponse.class);
								System.out.println(responseInfo.result);
								com.wonyesoft.wuliu.vo.State state = jrs.getState();
								if (state.getCode().equals(State.SUCEECCODE)) {

									ToastUtils.showLong(state.getMsg());
									finish();
								} else
									ToastUtils.showLong(state.getMsg());

							}

							@Override
							public void onFailure(HttpException error, String msg) {

								ToastUtils.showLong("更新失败");
							}
						});
			}

}
